package gui;

import gui.nodes.AbstractNode;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SpringLayout;

import persistence.db.DataSet;
import business.AnneeEtudeBL;

import com.toedter.calendar.JCalendar;

public class AnneeEtudeUIConsulter extends AbstractViewPane {

	private JTextField fieldNom;
	private JCalendar fieldDebut;
	private JCalendar fieldFin;
	private JTextArea areaDesc;
	private JSpinner spinMatin;
	private JSpinner spinAprem;
	private JSpinner spinDuree;
	
	private int id = -1;
	private int idFormation;
	private String nom;
	private String debut;
	private String fin;
	private String desc;
	private int matin;
	private int aprem;
	private int duree;
	private DataSet semestres; 
	private int dayDeb;
	private int monthDeb;
	private int yearDeb;
	private int dayFin;
	private int monthFin;
	private int yearFin;

	private ComponentList componentList = new ComponentList();
	private ViewMode currentViewMode;
	private AbstractNode creator;
	
	public AnneeEtudeUIConsulter(AbstractNode creator, ViewMode v, int idAnneeEtude, int formation) {
		this.creator = creator;
		currentViewMode = v;
		id = idAnneeEtude;
		idFormation = formation;
		initGUI(idAnneeEtude);
	}
	

	public void initGUI (int idAnneeEtude) {
        
        //si l'id du noeud envoyé est -1, on est en mode ajout, les champs seront donc vides
      
		if (currentViewMode != ViewMode.AJOUT) {
        	AnneeEtudeBL anneeEtudeBL = new AnneeEtudeBL(idAnneeEtude);
			nom = anneeEtudeBL.getNomAnneeEtude();
			debut = anneeEtudeBL.getDebutAnneeEtude().toString();
			fin = anneeEtudeBL.getFinAnneeEtude().toString();
			desc = anneeEtudeBL.getDescriptionAnneeEtude();
			matin = anneeEtudeBL.getNbSeancesaeMatin();
			aprem = anneeEtudeBL.getNbSeancesaeAprem();
			duree = anneeEtudeBL.getDureeSeance();
			semestres = anneeEtudeBL.getSemestresSet();
        }

		
        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());

        JLabel l = new JLabel("Nom d'année étude", JLabel.TRAILING);
        p.add(l);
        fieldNom = new JTextField(nom);
        componentList.add(fieldNom);
        
        l.setLabelFor(fieldNom);
        p.add(fieldNom);
        
        l = new JLabel("Début d'année étude : ", JLabel.TRAILING);
        p.add(l);
        if (currentViewMode != ViewMode.AJOUT) {
	    	String tmp = debut.replace('/',' ');
	    	StringTokenizer tokenizer = new StringTokenizer(tmp);
	   	    dayDeb = Integer.parseInt(tokenizer.nextToken());
	   	    monthDeb = Integer.parseInt(tokenizer.nextToken());
	   	    yearDeb = Integer.parseInt(tokenizer.nextToken());
	        Date date = new Date(yearDeb-1900, monthDeb-1, dayDeb);
	        fieldDebut = new JCalendar(date);
        }
        else
        	fieldDebut = new JCalendar();
        componentList.add(fieldDebut);
        		
        l.setLabelFor(fieldDebut);
        p.add(fieldDebut);

        l = new JLabel("Fin d'année étude : ", JLabel.TRAILING);
        p.add(l);
        if (currentViewMode != ViewMode.AJOUT) {
	        String tmp = fin.replace('/',' ');
	        StringTokenizer tokenizer = new StringTokenizer(tmp);
	   	    dayFin = Integer.parseInt(tokenizer.nextToken());
	   	    monthFin = Integer.parseInt(tokenizer.nextToken());
	   	    yearFin = Integer.parseInt(tokenizer.nextToken());
	        Date date = new Date(yearFin-1900, monthFin-1, dayFin);
	        fieldFin = new JCalendar(date);
        }
        else 
        	fieldFin = new JCalendar();
        componentList.add(fieldFin);
        
        l.setLabelFor(fieldFin);
        p.add(fieldFin);

        l = new JLabel("Description d'année étude : ", JLabel.TRAILING);
        p.add(l);
        areaDesc = new JTextArea(desc);
        componentList.add(areaDesc);
        
        l.setLabelFor(areaDesc);
        p.add(areaDesc);
        areaDesc.setRows(3);

        l = new JLabel("Nombre de séances du matin : ", JLabel.TRAILING);
        p.add(l);
        SpinnerModel sm = new SpinnerNumberModel(matin, 0, 10, 1);
        spinMatin = new JSpinner(sm);
        componentList.add(spinMatin);
        
        l.setLabelFor(spinMatin);
        p.add(spinMatin);

        l = new JLabel("Nombre de séances de l'après-midi : ", JLabel.TRAILING);
        p.add(l);
        sm = new SpinnerNumberModel(aprem, 0, 10, 1);
        spinAprem = new JSpinner(sm);
        componentList.add(spinAprem);
        
        l.setLabelFor(spinAprem);
        p.add(spinAprem);
        
        l = new JLabel("Durée de séance : ", JLabel.TRAILING);
        p.add(l);
        sm = new SpinnerNumberModel(duree, 0, 500, 1);
        spinDuree = new JSpinner(sm);
        componentList.add(spinDuree);
        
        l.setLabelFor(spinDuree);
        p.add(spinDuree);

        //Lay out the panel.
        SpringUtilities.makeCompactGrid(p,
                                        7, 2, //rows, cols
                                        6, 6,        //initX, initY
                                        6, 6);       //xPad, yPad
        
        add(p, BorderLayout.NORTH);
	    
        if (currentViewMode != ViewMode.CONSULTATION) {
        	// Mode modification - afficher les boutons
			showCommandBox();
        }
        else{
        	componentList.disableAll();
        }
        
	}
	
	private void showCommandBox() {
		JPanel commandBox = new JPanel();
		JButton save = new JButton("Enregistrer");
		save.setActionCommand("Enregistrer");
		save.addActionListener(new AnneeEtudeListener());
		commandBox.add(save);
		drawCommandBox(commandBox);
	}
	
	@Override
	public String getTitle() {
		// TODO Auto-generated method stub
		if (currentViewMode == ViewMode.CONSULTATION)
			return "Consultation d'un semestre";
		else if (currentViewMode == ViewMode.AJOUT)
			return "Ajout d'une année étude";
		else
			return "Modification d'une année étude";
	}

	@Override
	public boolean hasBeenModified() {
		// TODO Auto-generated method stub
		return (currentViewMode != ViewMode.CONSULTATION);
	}

	//inner class chargée de gérer l'écoute du bouton d'enregistrement du formulaire
	private class AnneeEtudeListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			if(arg0.getActionCommand().equals("Enregistrer")){
				System.out.println("Demande d'enregistrement");
				AnneeEtudeBL etudeSave = new AnneeEtudeBL();
				//si -1, c'est un ajout d'enseignant, sinon modification de l'enseignant de l'id concerné
				if(currentViewMode == ViewMode.AJOUT){
						etudeSave.ajouterAnneeEtude(getID(), getNom(), getDesc(), getDebut(), getFin(), getMatin(), getAprem(), getDuree(), getIdFormation(), getSemestres());
						componentList.disableAll();
				} else {
						etudeSave.modifierAnneeEtude(getID(), getNom(), getDesc(), getDebut(), getFin(), getMatin(), getAprem(), getDuree(), getIdFormation(), getSemestres());
						componentList.disableAll();
				}
				creator.refresh();
			}	
		}
	}
	
	//accesseurs
	public int getID() {
		return id;
	}
	
	public String getNom() {
		return fieldNom.getText();
	}

	public String getDebut() {
		return fieldDebut.getDayChooser().getDay()+"/"+fieldDebut.getMonthChooser().getMonth()+"/"+fieldDebut.getYearChooser().getYear();
	}

	public String getFin() {
		return fieldFin.getDayChooser().getDay()+"/"+fieldFin.getMonthChooser().getMonth()+"/"+fieldFin.getYearChooser().getYear();
	}

	public String getDesc() {
		return areaDesc.getText();
	}

	public int getMatin() {
		return Integer.parseInt(spinMatin.getValue().toString());
	}

	public int getAprem() {
		return Integer.parseInt(spinAprem.getValue().toString());
	}
	
	public int getDuree() {
		return Integer.parseInt(spinDuree.getValue().toString());
	}

	public int getIdFormation() {
		return idFormation;
	}
	
	public DataSet getSemestres(){
		return semestres;
	}

}
